package com.ruoyi.web.controller.api.city;

import cn.hutool.core.util.IdUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.cust.domain.CityCollect;
import com.ruoyi.cust.domain.CityProject;
import com.ruoyi.cust.domain.CityShare;
import com.ruoyi.cust.domain.CityUser;
import com.ruoyi.cust.service.ICityCollectService;
import com.ruoyi.cust.service.ICityProjectService;
import com.ruoyi.cust.service.ICityShareService;
import com.ruoyi.cust.service.ICityUserService;
import com.ruoyi.web.core.config.SearchRedisHelper;
import com.ruoyi.web.core.utils.UserRecentSearch;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Set;

@Api("个人中心")
@RequestMapping({"/api/city/mine"})
@RestController
/* loaded from: CityMineController.class */ public class CityMineController extends BaseController {
    @Autowired
    private ICityCollectService cityCollectService;
    @Autowired
    private ICityProjectService cityProjectService;
    @Autowired
    private ICityUserService    cityUserService;
    @Autowired
    private ICityShareService   cityShareService;
    @Resource
    private SearchRedisHelper   searchRedisHelper;
    @Resource
    private RedisTemplate       redisTemplate;

    @ApiOperation("redis删除KEY")
    @GetMapping({"/removeRedis"})
    public Boolean removeRedis(String key) {
        return this.redisTemplate.delete(key);
    }

    @ApiOperation("获取最近的搜索记录")
    @GetMapping({"/listRecentSearch"})
    public Set<UserRecentSearch> listRecentSearch(UserRecentSearch userRecentSearch) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("user_id", userRecentSearch.getUserId());
        CityUser cityUserDB = this.cityUserService.selectCityUserByUserId(userRecentSearch.getUserId());
        if (cityUserDB != null) {
            userRecentSearch.setUserId(cityUserDB.getUserId());
            userRecentSearch.setUnionId(cityUserDB.getUserId());
        }
        return this.searchRedisHelper.listRecentSearch(userRecentSearch);
    }

    @ApiOperation("项目分享列表")
    @GetMapping({"/shareList"})
    public TableDataInfo shareList(CityShare cityShare) {
        startPage();
        return getDataTable(this.cityProjectService.selectShareCityProjectList(cityShare.getUserId(),cityShare.getSearchValue()));
    }

    @ApiOperation("新增项目分享")
    @PostMapping({"/addCityShare"})
    public void add(@RequestBody CityShare cityShare) {
        List<CityShare> cityCollectList = this.cityShareService.selectCityShareList(cityShare);
        cityShare.setShareId(Long.valueOf(IdUtil.getSnowflake(1, 1).nextId()));
        if (!cityCollectList.isEmpty()) {
            CityProject  cityProject  = this.cityProjectService.selectCityProjectByProjectId(cityShare.getProjectId());
            QueryWrapper queryWrapper = new QueryWrapper();
            queryWrapper.eq("user_id", cityShare.getUserId());
            CityUser cityUser = this.cityUserService.selectCityUserByUserId(cityShare.getUserId());
            if (cityProject != null && cityUser != null) {
                cityShare.setNickName(cityUser.getNickName());
                cityShare.setPhonenumber(cityUser.getPhonenumber());
                cityShare.setProjectName(cityProject.getProjectName());
                cityShare.setProjectTitle(cityProject.getProjectTitle());
                cityShare.setUserType(0);
                cityShare.setProjectId(cityProject.getProjectId());
                cityShare.setUserId(cityUser.getUserId());
                cityProject.setProjectShare(Long.valueOf(cityProject.getProjectShare().intValue() + 1));
                this.cityProjectService.updateCityProject(cityProject);
                this.cityShareService.insertCityShare(cityShare);
            }
        }
    }

    @ApiOperation("删除项目分享")
    @DeleteMapping({"/{shareIds}"})
    public AjaxResult remove(@PathVariable Long[] shareIds) {
        return toAjax(this.cityShareService.deleteCityShareByShareIds(shareIds));
    }

    @ApiOperation("分页查询项目收藏列表")
    @GetMapping({"/collectList"})
    public TableDataInfo cityCollectList(CityCollect cityCollect) {
        startPage();
        return getDataTable(this.cityProjectService.selectCollectCityProjectList(cityCollect.getUserId(),cityCollect.getSearchValue()));
    }

    @ApiOperation("新增项目收藏")
    @PostMapping({"/addCollect"})
    public AjaxResult addCityCollect(@RequestBody CityCollect cityCollect) {
        cityCollect.setCollectId(Long.valueOf(IdUtil.getSnowflake(1, 1).nextId()));
        //查看是否已经被收藏
        if (!this.cityCollectService.selectCityCollectList(cityCollect).isEmpty()) {
            return new AjaxResult(0,"该项目已被收藏!");
        }
        CityProject  cityProject  = this.cityProjectService.selectCityProjectByProjectId(cityCollect.getProjectId());
        if(cityProject==null){
            return new AjaxResult(0,"该项目不存在！");
        }
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("user_id", cityCollect.getUserId());
        CityUser cityUser = this.cityUserService.selectCityUserByUserId(cityCollect.getUserId());
        if (!(cityProject == null || cityUser == null)) {
            cityCollect.setNickName(cityUser.getNickName());
            cityCollect.setPhonenumber(cityUser.getPhonenumber());
            cityCollect.setProjectName(cityProject.getProjectName());
            cityCollect.setProjectTitle(cityProject.getProjectTitle());
            cityCollect.setUserType(0);
            cityCollect.setProjectId(cityProject.getProjectId());
            cityCollect.setUserId(cityUser.getUserId());
            cityProject.setProjectCollect(Long.valueOf(cityProject.getProjectCollect().intValue() + 1));
            this.cityProjectService.updateCityProject(cityProject);
        }
        return toAjax(this.cityCollectService.insertCityCollect(cityCollect));
    }

    @ApiOperation("增加项目点击量")
    @GetMapping({"/addProjectHits"})
    public AjaxResult addProjectHits(@RequestParam("projectId") Long projectId) {
        CityProject cityProject = this.cityProjectService.selectCityProjectByProjectId(projectId);
        if (cityProject != null) {
            System.out.println("cityProject" + cityProject);
            cityProject.setProjectHits(Long.valueOf(cityProject.getProjectHits().intValue() + 1));
        }
        return toAjax(this.cityProjectService.updateCityProject(cityProject));
    }

    @ApiOperation("删除项目收藏")
    @DeleteMapping({"/delCollect/{projectId}"})
    public AjaxResult remove(@PathVariable Long projectId) {
        return toAjax(this.cityCollectService.deleteCityCollectByCollectId(projectId));
    }

    @ApiOperation("查看是否被收藏")
    @GetMapping({"/hisCollect/{projectId}/{userId}"})
    public Boolean hisCollect(@PathVariable Long projectId,@PathVariable Long userId) {
        return this.cityCollectService.collectHisByProjectId(projectId,userId);
    }

    @ApiOperation("查看浏览的项目列表-历史记录")
    @GetMapping({"/hisProject"})
    public List<CityProject> hisProject(@RequestParam("userId") Long userId,@RequestParam(value = "searchInfo",required = false) String searchInfo,
                                        @RequestParam("pageSize") Integer pageSize,@RequestParam("pageNum") Integer pageNum) {
        return this.cityCollectService.hisProject(userId,searchInfo,pageSize,pageNum);
    }
}
